import Vue from 'vue'
import store from '../store'
import VueRouter from 'vue-router'
import { AdminLayout, AccountLayout, PageView } from '../components/layouts'//
Vue.use(VueRouter)
/** 路由meta说明
 * title：路由标题
 * keep:页面缓存，页面保持
 */
const routes = [
  {
    path: '/',
    component: AdminLayout,
    redirect: '/home',
    children: [
      { path: '/home', name: 'Home', meta: { title: '首页' }, component: () => import('../views/Home') },
      {
        path: 'product',
        redirect: '/product/list',
        component: PageView,
        children: [
          { path: '/product/list', name: 'ProductList', meta: { title: '商品管理' }, component: () => import('../views/product/list') },
          { path: '/store/list', name: 'StoreList', meta: { title: '商家管理' }, component: () => import('../views/store/list') },
          { path: '/user/list', name: 'UserList', meta: { title: '用户管理' }, component: () => import('../views/user/list') },
          { path: '/shop-order/list', name: 'ShopOrderList', meta: { title: '订单管理' }, component: () => import('../views/shopOrder/list') },
          { path: '/recharge/list', name: 'RechargeList', meta: { title: '充值记录' }, component: () => import('../views/user/recharge-list') },
        ]
      },
    ]
  },
  {
    path: '/account',
    component: AccountLayout,
    children: [
      { path: '/login', name: 'Login', component: () => import('../views/account/login') },
      { path: 'register', name: 'Register', component: () => import('../views/account/register') }
    ]
  },

  { path: '/403', name: '403', component: () => import('../views/other/error/403') },
  { path: '/404', name: '404', component: () => import('../views/other/error/404') },
  { path: '/500', name: '500', component: () => import('../views/other/error/500') },
  { path: '*', name: 'All', component: () => import('../views/other/error/404') } //含有通配符的路由应该放在最后
]

const router = new VueRouter({ routes })

// 全局路由守卫
router.beforeEach((to, from, next) => {
  const _whiteList = ['Login', 'Register', '403', '404', '500', 'All'] //白名单name
  if (_whiteList.includes(to.name)) next()
  //已登录
  else if (store.state?.accessToken) next()
  else next({ name: 'Login', replace: true, query: { goto: to.name } })
})
export default router
